package com.haohan.stock.dao;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Sort;
import org.springframework.data.domain.Sort.Direction;
import org.springframework.data.domain.Sort.Order;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;
import org.springframework.stereotype.Component;

import com.haohan.stock.domain.StockBase;
import com.haohan.stock.vo.SpringDataPageable;

@Component
public class StockBaseDAO {

	@Autowired
	public MongoTemplate mongoTemplate;

	public List<StockBase> selectAll(String day, String code) {
		Criteria criteria = Criteria.where("day").is(day);
		criteria.andOperator(Criteria.where("code").is(code));
		Query query = Query.query(criteria);
		return mongoTemplate.find(query, StockBase.class);
	}

	public void saveAsMongo(StockBase stock) {
		boolean a = true;
		try {
			while (a) {
				Criteria criteria = Criteria.where("day").is(stock.getDay());
				criteria.andOperator(Criteria.where("code").is(stock.getCode()));
				Query query = Query.query(criteria);
				Update update = Update.update("name", stock.getName());
				update.set("modern_price", stock.getModern_price());// 最新
				update.set("increase", stock.getIncrease());// 涨幅(%)
				update.set("increase_price", stock.getIncrease_price());// 涨跌
				update.set("sum_hands", stock.getSum_hands());// 总手
				update.set("modern_hands", stock.getModern_hands());// 现手
				update.set("buy_price", stock.getBuy_price());// 买入价
				update.set("sell_price", stock.getSell_price());// 卖出价
				update.set("increase_speed", stock.getIncrease_speed());// 涨速(%)
				update.set("change_hands", stock.getChange_hands());// 换手(%)
				update.set("money", stock.getMoney());// 金额(亿为单位)
				update.set("pe", stock.getPe());// 市盈率
				update.set("insutry", stock.getInsutry());// 所属行业
				update.set("max_price", stock.getMax_price());// 最高
				update.set("min_price", stock.getMin_price());// 最低
				update.set("begin_price", stock.getBegin_price());// 开盘
				update.set("last_price", stock.getLast_price());// 昨收
				update.set("amplitude", stock.getAmplitude());// 振幅%
				update.set("vol_to", stock.getVol_to());// 量比
				update.set("committee", stock.getCommittee());// 委比%
				update.set("commission", stock.getCommission());// 委差
				update.set("avg_price", stock.getAvg_price());// 均价
				update.set("inside_dish", stock.getInside_dish());// 内盘
				update.set("outside_dish", stock.getOutside_dish());// 外盘
				update.set("internal_to_external_ratio", stock.getInternal_to_external_ratio());// 内外比
				update.set("buy_first_vol", stock.getBuy_first_vol());// 买一量
				update.set("sell_first_vol", stock.getSell_first_vol());// 卖一量
				update.set("pb", stock.getPb());// 市净率
				update.set("total_capital", stock.getTotal_capital());// 总股本
				update.set("total_marketvalue", stock.getTotal_marketvalue());// 总市值
				update.set("circulation_capital", stock.getCirculation_capital());// 流通股本
				update.set("circulation_marketvalue", stock.getCirculation_marketvalue());// 流通市值
				update.set("increase_3day", stock.getIncrease_3day());// 3 日涨幅%
				update.set("increase_6day", stock.getIncrease_6day());// 6 日涨幅%
				update.set("change_3day_hands", stock.getChange_3day_hands());// 3
																				// 日换手%
				update.set("change_6day_hands", stock.getChange_6day_hands());// 6
																				// 日换手%
				mongoTemplate.upsert(query, update, StockBase.class);
				a = false;
			}
		} catch (Exception e) {
		}
	}

	public List<StockBase> selectResult(String day, int scale) {
		Criteria criteria = Criteria.where("day").is(day);
		criteria.andOperator(Criteria.where("scale").is(scale), Criteria.where("buy_point").is(true));
		Query query = Query.query(criteria);
		List<Order> orders = new ArrayList<Order>(); // 排序
		orders.add(new Order(Direction.DESC, "day"));
		Sort sort = new Sort(orders);
		SpringDataPageable pageable = new SpringDataPageable();
		pageable.setSort(sort);
		return mongoTemplate.find(query.with(pageable), StockBase.class);
	}
}
